// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/parser.h>
#include <cantor/transform/ddls/partition_option.h>
#include <cantor/transform/ddls/partition_range.h>
#include <cantor/transform/exprs/constant_expr.h>
#include <cantor/transform/exprs/index_lock_opt.h>
#include <cantor/transform/expr.h>

#include "cantor/transform/exprs/algorithm_opt.h"

namespace cantor::transform {

    void TransPartitionRange::resolve_ast(const cantor::parser::PartitionRange*node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }
        partition_range.set_name(node->name.to_string());
        if (node->less_expr) {
            status = TransExpr::resolve_to(node->less_expr,  partition_range.mutable_less_expr());
            if (!status.ok()) {
                return;
            }
        }
        if (node->range.first) {
            status = TransExpr::resolve_to(node->range.first,  partition_range.mutable_low_expr());
            if (!status.ok()) {
                return;
            }
        }
        if (node->range.second) {
            status = TransExpr::resolve_to(node->range.second,  partition_range.mutable_high_expr());
            if (!status.ok()) {
                return;
            }
        }
        partition_range.mutable_options()->Reserve(node->options.size());
        for (auto i = 0; i < node->options.size(); ++i) {
            TransPartitionOption opt;
            opt.resolve_ast(node->options[i]);
            status = opt.check();
            if (!status.ok()) {
                return;
            }
            auto ptr = partition_range.mutable_options()->Add();
            *ptr = std::move(opt.partition_option);
        }

        set_proto_location(node, partition_range.mutable_location());
        set_proto_status(partition_range.mutable_status());
    }

    const turbo::Status &TransPartitionRange::check() {
        if (!status.ok()) {
            return status;
        }
        if (!partition_range.has_location()) {
            make_missing_required_field("location");
            return status;
        }
        return status;
    }

    turbo::Status TransPartitionRange::resolve_to(turbo::Nonnull<const cantor::parser::PartitionRange*> arg, turbo::Nonnull<pb::ast::PartitionRange *>out) {
        TransPartitionRange trans;
        trans.resolve_ast(arg);
        auto status = trans.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(trans.partition_range);
        return status;
    }
}  // namespace cantor::transform
